<template>
<div>
  <el-select v-model="choose_course" placeholder="请选择">
    <el-option
        v-for="item in courses"
        :key="item.id"
        :label="item.courseName"
        :value="item.id">
    </el-option>
  </el-select>
  <el-button type="primary" style="margin-left: 10px" @click="getScores">确定</el-button>

  <el-table
      :data="scores"
      border
  style="margin-top: 20px;width: 1000px">
    <el-table-column
        prop="name"
        label="学生"
        width="180"
    >
    </el-table-column>
    <el-table-column
        prop="className"
        label="班级"
        width="180"
    >
    </el-table-column>
    <el-table-column
        prop="account"
        label="学号"
        width="180"
    >
    </el-table-column>
    <el-table-column
        prop="courseName"
        label="课程"
        >
    </el-table-column>
    <el-table-column
        prop="num"
        label="分数"
        width="180">
    </el-table-column>
    <el-table-column
        label="操作"
       >
      <template slot-scope="scope">
        <el-button type="text" @click="toUpdate(scope.row)" >编辑</el-button>
      </template>
    </el-table-column>
  </el-table>

  <el-dialog
  :visible.sync="isUpdate"
  >
    <el-form>
      <el-form-item>
        <el-input oninput="value = value.replace(/[^\d]/g,'')" v-model="updateScore"/>
        <el-button type="primary" @click="update">确定</el-button>
        <el-button @click="isUpdate = false" >取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</div>
</template>

<script>
export default {
  name: "ScoreGive",
  data(){
    return{
      choose_course:'',
      courses: [],
      scores: [],
      updateUserId: '',
      isUpdate: false,
      updateScore: 0
    }
  },
  methods:{
    toUpdate(student) {
      this.updateUserId = student.id
      this.updateScore = student.num
      this.isUpdate = true
    },
    update(){
      let param = {
        userId : this.updateUserId,
        num: this.updateScore,
        courseId: this.choose_course
      }
      this.getRequest('/updateScore',param).then(resp=>{
        if (resp.status==200){
          this.getScores()
          this.isUpdate = false
        }
      })
    },
    getScores(){
      let param = {
        courseId : this.choose_course,
        classId : JSON.parse(sessionStorage.getItem("user")).classId
      }
      this.getRequest('/getUserByClassAndCourse',param).then(resp=>{
        this.scores  = resp.data
      })
    }
  },
  created() {
    let user = JSON.parse(sessionStorage.getItem("user"))
    this.getRequest('/getCourses?classId='+user.classId).then(resp=>{
      this.courses = resp.data
      console.log(resp.data)
    })
  }
}
</script>

<style scoped>

</style>